from typing import *
from leetcode.editor.common.node import *


# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        if max(nums) ==0:
            return 0
        # 题意 ： 最长只包含一个或者不包含0的子数组
        window = {}
        win_sum = 0
        left = 0
        ans = 0
        for right ,item in enumerate(nums):
            window[item] = window.get(item,0)+1
            win_sum+=item
            if 0 not in window or  window[0] <=1:
                ans = max(ans,win_sum)
            while 0 in window and window[0]>1:
                l = nums[left]
                window[l]-=1
                win_sum -=l
                if window[0] <=1:
                    ans = max(ans,win_sum)
                left +=1
        return ans if 0 in window else ans-1
# leetcode submit region end(Prohibit modification and deletion)


if __name__ == '__main__':
    solution = Solution()
    print(solution.longestSubarray([0,0,0]))
    # your test code here